#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void Init(Stack* stack) {
	assert(stack);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}

void Push(Stack* stack, valuetype value) {
	assert(stack);
	if (stack->top == stack->capacity) {
		stack->capacity = stack->capacity == 0 ? 10 : (int)(stack->capacity * 1.5);
		stack->arr = (valuetype*)realloc(stack->arr, sizeof(valuetype) * stack->capacity);
		if (stack->arr == NULL) {
			perror("realloc failed in the function Push(Stack*, valuetype).");
			return;
		}
	}
	stack->arr[stack->top++] = value;
}

void Pop(Stack* stack) {
	assert(stack && stack->top > 0);
	stack->top--;
}

valuetype Top(Stack* stack) {
	assert(stack && stack->top > 0);
	return stack->arr[stack->top - 1];
}

int Size(Stack* stack) {
	assert(stack);
	return stack->top;
}

bool Empty(Stack* stack) {
	assert(stack);
	return stack->top == 0;
}

void Destroy(Stack* stack) {
	assert(stack);
	free(stack->arr);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}